<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>公告管理</title>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        :root {
            --primary-color: #00a896;
            --secondary-color: #02c39a;
            --dark-color: #05668d;
            --light-color: #f0f3bd;
            --accent-color: #028090;
            --text-dark: #333;
            --text-light: #f8f9fa;
            --shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
            --transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Noto Sans SC', sans-serif;
            color: var(--text-dark);
            background-color: #f9f9f9;
            min-height: 100vh;
            display: flex;
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 250px;
            background: linear-gradient(135deg, var(--dark-color) 0%, var(--primary-color) 100%);
            color: white;
            padding: 20px 0;
            height: 100vh;
            position: fixed;
            box-shadow: var(--shadow);
            z-index: 100;
        }

        .sidebar-header {
            padding: 0 20px 20px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
        }

        .sidebar-header .logo {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
        }

        .sidebar-header .logo-icon {
            width: 40px;
            height: 40px;
            background-color: rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 10px;
            font-size: 18px;
            color: var(--light-color);
        }

        .sidebar-header h3 {
            font-size: 1.2rem;
            font-weight: 500;
            margin: 0;
        }

        .sidebar-header p {
            font-size: 0.8rem;
            opacity: 0.8;
            margin: 0;
        }

        .nav-menu {
            list-style: none;
            padding: 0 15px;
        }

        .nav-item {
            margin-bottom: 5px;
        }

        .nav-link {
            display: flex;
            align-items: center;
            padding: 12px 15px;
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            border-radius: 8px;
            transition: var(--transition);
        }

        .nav-link:hover, .nav-link.active {
            background-color: rgba(255, 255, 255, 0.1);
            color: white;
        }

        .nav-link i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
            font-size: 16px;
        }

        .nav-link .badge {
            margin-left: auto;
            background-color: var(--light-color);
            color: var(--dark-color);
            font-size: 0.7rem;
            padding: 3px 6px;
        }

        .user-profile {
            position: absolute;
            bottom: 20px;
            left: 0;
            width: 100%;
            padding: 0 20px;
        }

        .user-info {
            display: flex;
            align-items: center;
            padding: 10px;
            background-color: rgba(255, 255, 255, 0.1);
            border-radius: 8px;
            cursor: pointer;
            transition: var(--transition);
        }

        .user-info:hover {
            background-color: rgba(255, 255, 255, 0.2);
        }

        .user-avatar {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            background-color: rgba(255, 255, 255, 0.2);
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 10px;
            font-size: 14px;
        }

        .user-name {
            font-size: 0.9rem;
            font-weight: 500;
            margin-bottom: 2px;
        }

        .user-role {
            font-size: 0.7rem;
            opacity: 0.8;
        }

        /* 主内容区 */
        .main-content {
            flex: 1;
            margin-left: 250px;
            padding: 30px;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }

        .page-title h1 {
            font-size: 1.8rem;
            font-weight: 600;
            color: var(--dark-color);
            margin-bottom: 5px;
        }

        .page-title p {
            color: #777;
            font-size: 0.9rem;
            margin: 0;
        }

        .header-actions {
            display: flex;
            gap: 10px;
        }

        .btn {
            padding: 8px 16px;
            border-radius: 8px;
            font-size: 0.9rem;
            font-weight: 500;
            border: none;
            cursor: pointer;
            transition: var(--transition);
            display: flex;
            align-items: center;
        }

        .btn i {
            margin-right: 6px;
        }

        .btn-primary {
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            color: white;
            box-shadow: 0 4px 15px rgba(0, 168, 150, 0.3);
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(0, 168, 150, 0.4);
        }

        .btn-outline {
            background: transparent;
            border: 1px solid #ddd;
            color: #555;
        }

        .btn-outline:hover {
            background-color: #f5f5f5;
        }

        /* 搜索和过滤区域 */
        .filter-section {
            background-color: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: var(--shadow);
            margin-bottom: 20px;
        }

        .filter-row {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            margin-bottom: 15px;
        }

        .filter-group {
            flex: 1;
            min-width: 200px;
        }

        .filter-group label {
            display: block;
            margin-bottom: 8px;
            font-size: 0.9rem;
            color: #555;
            font-weight: 500;
        }

        .filter-control {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 0.9rem;
            transition: var(--transition);
        }

        .filter-control:focus {
            border-color: var(--primary-color);
            outline: none;
            box-shadow: 0 0 0 2px rgba(0, 168, 150, 0.1);
        }

        .filter-actions {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            padding-top: 10px;
            border-top: 1px solid #eee;
        }

        /* 表格样式 */
        .card {
            background-color: white;
            border-radius: 12px;
            box-shadow: var(--shadow);
            padding: 20px;
            margin-bottom: 20px;
        }

        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }

        .card-header h3 {
            font-size: 1.2rem;
            font-weight: 600;
            margin: 0;
            color: var(--dark-color);
        }

        .table-responsive {
            overflow-x: auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }

        th {
            font-weight: 500;
            color: #555;
            font-size: 0.9rem;
            background-color: #f9f9f9;
        }

        tr:hover {
            background-color: #f5f5f5;
        }

        .badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 0.75rem;
            font-weight: 500;
        }

        .badge-success {
            background-color: #e8f5e9;
            color: #4caf50;
        }

        .badge-warning {
            background-color: #fff8e1;
            color: #ff9800;
        }

        .badge-danger {
            background-color: #ffebee;
            color: #f44336;
        }

        .badge-info {
            background-color: #e3f2fd;
            color: #2196f3;
        }

        .badge-primary {
            background-color: #e0f2f1;
            color: var(--primary-color);
        }

        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
            gap: 5px;
        }

        .page-item {
            list-style: none;
        }

        .page-link {
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 6px;
            color: #555;
            text-decoration: none;
            transition: var(--transition);
        }

        .page-link:hover {
            background-color: #f5f5f5;
        }

        .page-item.active .page-link {
            background-color: var(--primary-color);
            color: white;
            border-color: var(--primary-color);
        }

        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }

        .modal-content {
            background-color: white;
            margin: 5% auto;
            padding: 20px;
            border-radius: 12px;
            width: 60%;
            max-width: 800px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            animation: modalFadeIn 0.3s ease-out;
        }

        @keyframes modalFadeIn {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #eee;
            padding-bottom: 15px;
            margin-bottom: 15px;
        }

        .modal-header h3 {
            margin: 0;
            font-size: 1.3rem;
            color: var(--dark-color);
        }

        .modal-close {
            background: none;
            border: none;
            font-size: 1.5rem;
            cursor: pointer;
            color: #777;
        }

        .modal-body {
            padding: 20px;
        }

        .modal-footer {
            padding: 15px 20px;
            border-top: 1px solid #eee;
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
        }

        .form-control {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        textarea.form-control {
            min-height: 100px;
        }

        .attachment-item {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
            gap: 10px;
        }

        .attachment-item input[type="text"] {
            flex: 1;
        }

        /* 响应式设计 */
        @media (max-width: 992px) {
            .sidebar {
                width: 70px;
                overflow: hidden;
            }

            .sidebar-header h3,
            .sidebar-header p,
            .nav-link span,
            .user-name,
            .user-role {
                display: none;
            }

            .sidebar-header {
                padding: 15px 10px;
            }

            .nav-link {
                justify-content: center;
                padding: 12px 0;
            }

            .nav-link i {
                margin-right: 0;
                font-size: 18px;
            }

            .main-content {
                margin-left: 70px;
            }
        }

        @media (max-width: 768px) {
            .filter-row {
                flex-direction: column;
                gap: 10px;
            }

            .filter-group {
                min-width: 100%;
            }

            .header {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }
        }

        @media (max-width: 576px) {
            .main-content {
                padding: 20px 15px;
            }

            th, td {
                padding: 8px 10px;
                font-size: 0.85rem;
            }

            .modal-content {
                width: 95%;
            }
        }
    </style>
</head>
<body>
<!-- 侧边导航栏 -->
<div class="sidebar">
    <div class="sidebar-header">
        <div class="logo">
            <div class="logo-icon">
                <i class="fas fa-charging-station"></i>
            </div>
            <div>
                <h3>绿能充</h3>
                <p>管理员面板</p>
            </div>
        </div>
    </div>

    <ul class="nav-menu">
        <li class="nav-item">
            <a href="/admin/dashboard" class="nav-link">
                <i class="fas fa-tachometer-alt"></i>
                <span>仪表盘</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/admin/user/list" class="nav-link">
                <i class="fas fa-users"></i>
                <span>用户管理</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/admin/pile/list" class="nav-link">
                <i class="fas fa-charging-station"></i>
                <span>充电桩管理</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/admin/announcement/list" class="nav-link active">
                <i class="fas fa-bullhorn"></i>
                <span>公告管理</span>
                <span class="badge">${newAnnouncementCount}</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/admin/report/list" class="nav-link">
                <i class="fas fa-list-alt"></i>
                <span>充电记录</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/admin/fault/list" class="nav-link">
                <i class="fas fa-exclamation-triangle"></i>
                <span>故障报告</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/admin/settings" class="nav-link">
                <i class="fas fa-cog"></i>
                <span>系统设置</span>
            </a>
        </li>
    </ul>

    <div class="user-profile">
        <div class="user-info">
            <div class="user-avatar">
                <i class="fas fa-user"></i>
            </div>
            <div>
                <div class="user-name">管理员</div>
                <div class="user-role">超级管理员</div>
            </div>
        </div>
    </div>
</div>

<!-- 主内容区 -->
<div class="main-content">
    <div class="header">
        <div class="page-title">
            <h1>公告管理</h1>
            <p>管理系统公告，共 <strong>${totalAnnouncements}</strong> 条公告</p>
        </div>
        <div class="header-actions">
            <button class="btn btn-primary">
                <a href="${pageContext.request.contextPath}/admin/announcement/add">
                    <i class="fas fa-plus"></i> 新增公告
                </a>
            </button>
            <button class="btn btn-outline">
                <i class="fas fa-download"></i> 导出
            </button>
        </div>
    </div>

    <!-- 搜索和过滤区域 -->
    <div class="filter-section">
        <form id="searchForm">
            <div class="filter-row">
                <div class="filter-group">
                    <label for="searchKeyword">关键词</label>
                    <input type="text" id="searchKeyword" class="filter-control" placeholder="标题或内容..." name="keyword">
                </div>
                <div class="filter-group">
                    <label for="searchType">公告类型</label>
                    <select id="searchType" class="filter-control" name="type">
                        <option value="">全部类型</option>
                        <option value="SYSTEM">系统公告</option>
                        <option value="MAINTENANCE">维护公告</option>
                        <option value="PROMOTION">促销公告</option>
                        <option value="OTHER">其他</option>
                    </select>
                </div>
                <div class="filter-group">
                    <label for="searchStatus">状态</label>
                    <select id="searchStatus" class="filter-control" name="status">
                        <option value="">全部状态</option>
                        <option value="PUBLISHED">已发布</option>
                        <option value="DRAFT">草稿</option>
                        <option value="ARCHIVED">已归档</option>
                    </select>
                </div>
            </div>
            <div class="filter-row">
                <div class="filter-group">
                    <label for="startDate">开始时间</label>
                    <input type="date" id="startDate" class="filter-control" name="startDate">
                </div>
                <div class="filter-group">
                    <label for="endDate">结束时间</label>
                    <input type="date" id="endDate" class="filter-control" name="endDate">
                </div>
                <div class="filter-group">
                    <label>&nbsp;</label>
                    <div style="display: flex; gap: 10px;">
                        <button type="button" class="btn btn-outline" onclick="resetSearch()">重置</button>
                        <button type="submit" class="btn btn-primary">搜索</button>
                    </div>
                </div>
            </div>
        </form>
    </div>

    <!-- 公告表格 -->
    <div class="card">
        <div class="card-header">
            <h3>公告列表</h3>
            <div class="header-actions">
                <select class="form-control form-select" style="width: auto; margin-right: 10px;" onchange="changePageSize(this.value)">
                    <option value="10">每页10条</option>
                    <option value="25">每页25条</option>
                    <option value="50">每页50条</option>
                </select>
            </div>
        </div>
        <div class="table-responsive">
            <table class="table">
                <thead>
                <tr>
                    <th width="5%">ID</th>
                    <th width="20%">标题</th>
                    <th width="10%">类型</th>
                    <th width="15%">发布人</th>
                    <th width="15%">开始时间</th>
                    <th width="15%">结束时间</th>
                    <th width="10%">状态</th>
                    <th width="10%">操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${announcementList}" var="announcement">
                    <tr>
                        <td>${announcement.id}</td>
                        <td>${announcement.title}</td>
                        <td>
                            <c:choose>
                                <c:when test="${announcement.type == 'SYSTEM'}">系统公告</c:when>
                                <c:when test="${announcement.type == 'MAINTENANCE'}">维护公告</c:when>
                                <c:when test="${announcement.type == 'PROMOTION'}">促销公告</c:when>
                                <c:when test="${announcement.type == 'OTHER'}">其他</c:when>
                                <c:otherwise>${announcement.type}</c:otherwise>
                            </c:choose>
                        </td>
                        <td>${announcement.publisherId}</td>
                        <td>${announcement.startTime}</td>
                        <td>${announcement.endTime}</td>
                        <td>
                            <c:choose>
                                <c:when test="${announcement.status == 'PUBLISHED'}">
                                    <span class="badge badge-success">已发布</span>
                                </c:when>
                                <c:when test="${announcement.status == 'DRAFT'}">
                                    <span class="badge badge-warning">草稿</span>
                                </c:when>
                                <c:when test="${announcement.status == 'ARCHIVED'}">
                                    <span class="badge badge-danger">已归档</span>
                                </c:when>
                            </c:choose>
                        </td>
                        <td>
                            <button class="btn btn-outline btn-sm">
                                <a href="${pageContext.request.contextPath}/admin/announcement/detail/${announcement.id}">
                                    <i class="fas fa-eye"></i>
                                </a>
                            </button>
                            <button class="btn btn-outline btn-sm">
                                <a href="${pageContext.request.contextPath}/admin/announcement/delete/${announcement.id}">
                                    <i class="fas fa-trash"></i>
                                </a>
                            </button>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>

        <!-- 分页 -->
        <nav class="pagination">
            <c:if test="${currentPage > 1}">
                <li class="page-item">
                    <a class="page-link" href="?page=${currentPage - 1}">&laquo;</a>
                </li>
            </c:if>

            <c:forEach begin="1" end="${totalPages}" var="pageNum">
                <li class="page-item ${pageNum == currentPage ? 'active' : ''}">
                    <a class="page-link" href="?page=${pageNum}">${pageNum}</a>
                </li>
            </c:forEach>

            <c:if test="${currentPage < totalPages}">
                <li class="page-item">
                    <a class="page-link" href="?page=${currentPage + 1}">&raquo;</a>
                </li>
            </c:if>
        </nav>
    </div>
</div>

<!-- 查看公告模态框 -->
<div id="viewModal" class="modal">
    <div class="modal-content">
        <div class="modal-header">
            <h3 id="viewTitle">公告详情</h3>
            <button class="modal-close" onclick="closeViewModal()">&times;</button>
        </div>
        <div class="modal-body">
            <div class="form-group">
                <label>标题</label>
                <p id="viewTitleContent"></p>
            </div>

            <div class="form-group">
                <label>类型</label>
                <p id="viewType"></p>
            </div>

            <div class="form-group">
                <label>发布人</label>
                <p id="viewPublisher"></p>
            </div>

            <div class="form-group">
                <label>开始时间</label>
                <p id="viewStartTime"></p>
            </div>

            <div class="form-group">
                <label>结束时间</label>
                <p id="viewEndTime"></p>
            </div>

            <div class="form-group">
                <label>状态</label>
                <p id="viewStatus"></p>
            </div>

            <div class="form-group">
                <label>内容</label>
                <div id="viewContent" style="padding: 10px; border: 1px solid #ddd; border-radius: 4px;"></div>
            </div>

            <div class="form-group">
                <label>附件</label>
                <div id="viewAttachments">
                    <!-- 附件列表将在这里动态生成 -->
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary" onclick="closeViewModal()">关闭</button>
        </div>
    </div>
</div>

<script>
    // 打开添加公告模态框
    function openAddModal() {
        document.getElementById('modalTitle').textContent = '新增公告';
        document.getElementById('announcementId').value = '';
        document.getElementById('announcementForm').reset();
        document.getElementById('attachmentsList').innerHTML = '';
        document.getElementById('announcementModal').style.display = 'block';
    }

    // 查看公告详情
    function viewAnnouncement(id) {
        // 这里应该是AJAX请求获取公告详情
        // 模拟数据
        const announcement = {
            id: id,
            title: '系统维护通知',
            type: 'MAINTENANCE',
            publisher: { username: '管理员' },
            content: '系统将于2023-06-15 00:00至2023-06-15 06:00进行维护升级，届时系统将无法访问，请提前做好安排。',
            startTime: '2023-06-15T00:00',
            endTime: '2023-06-15T06:00',
            status: 'PUBLISHED',
            attachments: [
                {id: 1, name: '维护计划.pdf', url: '/attachments/1.pdf'},
                {id: 2, name: '升级说明.docx', url: '/attachments/2.docx'}
            ]
        };

        document.getElementById('viewTitleContent').textContent = announcement.title;
        document.getElementById('viewType').textContent = getTypeName(announcement.type);
        document.getElementById('viewPublisher').textContent = announcement.publisher.username;
        document.getElementById('viewStartTime').textContent = formatDateTime(announcement.startTime);
        document.getElementById('viewEndTime').textContent = formatDateTime(announcement.endTime);
        document.getElementById('viewContent').innerHTML = announcement.content.replace(/\n/g, '<br>');

        // 状态显示
        let statusElement = document.getElementById('viewStatus');
        statusElement.textContent = getStatusName(announcement.status);
        statusElement.className = 'badge badge-' + (announcement.status === 'PUBLISHED' ? 'success' :
            announcement.status === 'DRAFT' ? 'warning' : 'danger');

        // 显示附件
        const attachmentsContainer = document.getElementById('viewAttachments');
        attachmentsContainer.innerHTML = '';
        if (announcement.attachments && announcement.attachments.length > 0) {
            announcement.attachments.forEach(attachment => {
                const attachmentItem = document.createElement('div');
                attachmentItem.className = 'attachment-item';
                attachmentItem.innerHTML = `
                    <i class="fas fa-paperclip"></i>
                    <span>${attachment.name}</span>
                    <a href="${attachment.url}" target="_blank" class="btn btn-outline btn-sm">
                        <i class="fas fa-download"></i> 下载
                    </a>
                `;
                attachmentsContainer.appendChild(attachmentItem);
            });
        } else {
            attachmentsContainer.innerHTML = '<p>无附件</p>';
        }

        document.getElementById('viewModal').style.display = 'block';
    }

    // 编辑公告
    function editAnnouncement(id) {
        // 这里应该是AJAX请求获取公告数据
        // 模拟数据
        const announcement = {
            id: id,
            title: '系统维护通知',
            type: 'MAINTENANCE',
            content: '系统将于2023-06-15 00:00至2023-06-15 06:00进行维护升级，届时系统将无法访问，请提前做好安排。',
            startTime: '2023-06-15T00:00',
            endTime: '2023-06-15T06:00',
            status: 'PUBLISHED',
            attachments: [
                {id: 1, name: '维护计划.pdf', url: '/attachments/1.pdf'}
            ]
        };

        document.getElementById('modalTitle').textContent = '编辑公告';
        document.getElementById('announcementId').value = announcement.id;
        document.getElementById('title').value = announcement.title;
        document.getElementById('type').value = announcement.type;
        document.getElementById('content').value = announcement.content;
        document.getElementById('startTime').value = announcement.startTime;
        document.getElementById('endTime').value = announcement.endTime;
        document.getElementById('status').value = announcement.status;

        // 显示附件
        const attachmentsList = document.getElementById('attachmentsList');
        attachmentsList.innerHTML = '';
        if (announcement.attachments && announcement.attachments.length > 0) {
            announcement.attachments.forEach(attachment => {
                const attachmentItem = document.createElement('div');
                attachmentItem.className = 'attachment-item';
                attachmentItem.innerHTML = `
                    <input type="text" name="attachmentName" value="${attachment.name}" class="form-control" placeholder="附件名称" style="flex: 2;">
                    <input type="text" name="attachmentUrl" value="${attachment.url}" class="form-control" placeholder="URL" style="flex: 3;">
                    <button type="button" class="btn btn-outline btn-sm" onclick="removeAttachment(this)">
                        <i class="fas fa-times"></i>
                    </button>
                `;
                attachmentsList.appendChild(attachmentItem);
            });
        }

        document.getElementById('announcementModal').style.display = 'block';
    }

    // 删除公告
    function deleteAnnouncement(id) {
        if (confirm('确定要删除这条公告吗？')) {
            // 这里应该是AJAX请求删除公告
            alert('公告删除成功');
            // 刷新列表
            location.reload();
        }
    }

    // 添加附件
    function addAttachment() {
        const attachmentsList = document.getElementById('attachmentsList');
        const attachmentItem = document.createElement('div');
        attachmentItem.className = 'attachment-item';
        attachmentItem.innerHTML = `
            <input type="text" name="attachmentName" class="form-control" placeholder="附件名称" style="flex: 2;">
            <input type="text" name="attachmentUrl" class="form-control" placeholder="URL" style="flex: 3;">
            <button type="button" class="btn btn-outline btn-sm" onclick="removeAttachment(this)">
                <i class="fas fa-times"></i>
            </button>
        `;
        attachmentsList.appendChild(attachmentItem);
    }

    // 移除附件
    function removeAttachment(button) {
        button.parentElement.remove();
    }

    // 关闭模态框
    function closeModal() {
        document.getElementById('announcementModal').style.display = 'none';
    }

    function closeViewModal() {
        document.getElementById('viewModal').style.display = 'none';
    }

    // 表单提交
    document.getElementById('announcementForm').addEventListener('submit', function(e) {
        e.preventDefault();
        // 这里应该是AJAX请求保存公告
        alert('公告保存成功');
        closeModal();
        // 刷新列表
        location.reload();
    });

    // 辅助函数：格式化日期时间
    function formatDateTime(dateTimeStr) {
        if (!dateTimeStr) return '';
        const date = new Date(dateTimeStr);
        return date.toLocaleString();
    }

    // 辅助函数：获取类型名称
    function getTypeName(type) {
        const types = {
            'SYSTEM': '系统公告',
            'MAINTENANCE': '维护公告',
            'PROMOTION': '促销公告',
            'OTHER': '其他'
        };
        return types[type] || type;
    }

    // 辅助函数：获取状态名称
    function getStatusName(status) {
        const statuses = {
            'PUBLISHED': '已发布',
            'DRAFT': '草稿',
            'ARCHIVED': '已归档'
        };
        return statuses[status] || status;
    }

    // 重置搜索表单
    function resetSearch() {
        document.getElementById('searchForm').reset();
    }

    // 改变每页显示数量
    function changePageSize(size) {
        window.location.href = `?pageSize=${size}&page=1`;
    }

    // 点击模态框外部关闭模态框
    window.onclick = function(event) {
        if (event.target.className === 'modal') {
            closeModal();
            closeViewModal();
        }
    }
</script>
</body>
</html>